﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace XsdSort
{
    public class XsdReorderProcessor
    {
        // 存储结构：外层按complexType名称排序，内层按element名称排序
        private readonly SortedDictionary<string, SortedDictionary<string, string>> _schemaData
            = new SortedDictionary<string, SortedDictionary<string, string>>();

        public void ProcessXsdFile(string inputPath, string outputPath)
        {
            // 逐行读取并解析
            var reader = XmlReader.Create(inputPath);
            string currentComplexType = null;
            var currentElements = new List<(string name, string content)>();
            var settings = new XmlWriterSettings
            {
                ConformanceLevel = ConformanceLevel.Fragment, // フラグメントモードに変更
                Indent = true
            };
            // 写入排序后的结果
            var writer = XmlWriter.Create(outputPath, settings);
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "complexType")
                {
                    if (currentComplexType != null)
                    {
                        StoreCurrentType(currentComplexType, currentElements);
                    }
                    currentComplexType = reader.GetAttribute("name");
                    currentElements = new List<(string, string)>();
                }
                else if (currentComplexType != null && reader.NodeType == XmlNodeType.Element && reader.LocalName == "element")
                {
                    var subtree = reader.ReadSubtree();
                    subtree.Read();
                    currentElements.Add((subtree.GetAttribute("name"), subtree.ReadOuterXml()));
                }
                else 
                {
                    writer.WriteString(reader.Value);
                }
            }
            StoreCurrentType(currentComplexType, currentElements); // 存储最后一个类型


            writer.WriteStartDocument();
            //writer.WriteStartElement("xs", "schema", "http://www.w3.org/2001/XMLSchema");

            foreach (var type in _schemaData)
            {
                writer.WriteStartElement("xs", "complexType", null);
                writer.WriteAttributeString("name", type.Key);

                writer.WriteStartElement("xs", "all", null);
                foreach (var element in type.Value)
                {
                    writer.WriteRaw(element.Value); // 保持原始XML结构
                }
                writer.WriteEndElement();
            }

            //writer.WriteEndElement();
            writer.WriteEndDocument();
        }

        private void StoreCurrentType(string complexType, List<(string name, string content)> elements)
        {
            var sortedElements = new SortedDictionary<string, string>();
            foreach (var element in elements.OrderBy(e => e.name))
            {
                sortedElements[element.name] = element.content;
            }
            _schemaData[complexType] = sortedElements;
        }
    }
}
